package com.gxlu.ms.base.result;

import java.io.Serializable;
import java.util.Map;

import com.gxlu.ms.base.util.JsonUtil;
import com.gxlu.ms.base.util.StringUtil;

/**
 * 结果类，可用于hsf,dubbo服务或者业务服务
 * @author andy.lhc
 * @param <T>
 */
public class Result<T> implements Serializable {

	private static final long serialVersionUID = 1L;
	/**
     * 结果码
     */
    protected int code;
    /**
     * errorMsg
     */
    protected String msg;

    /**
     * data
     */
    protected T data;

    /**
     * 状态
     */
    private Boolean status = false;


    /**
     * 附加属性，建议是map格式，即key，value格式
     */
    protected Map<String,Object> feature;

    public Result() {
    }

    public static <R extends Result> R success(){
        Result result = new Result();
        result.setStatus(true);
        result.setCode(1);
        return (R)result;
    }

    public static Result success(Map<String,Object> feature){
        Result result = new Result();
        result.setStatus(true);
        result.setFeature(feature);
        return result;
    }

    public static Result of(boolean status,int code,String msg){
        Result result = new Result();
        result.setStatus(status);
        result.setCode(code);
        result.setMsg(msg);
        return result ;
    }

    public static Result fail(int code,String msg){
        Result result = new Result();
        result.setStatus(false);
        result.setCode(code);
        result.setMsg(msg);
        return result ;
    }

    public static Result fail(int code,String msg,Map<String,Object> feature){
        Result result = new Result();
        result.setStatus(false);
        result.setCode(code);
        result.setMsg(msg);
        result.setFeature(feature);
        return result ;
    }

    public Result(boolean status,int code, String msg, T data) {
        this.status = status ;
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public Result(boolean status,int code, String msg, T data, Map<String,Object> feature) {
        this.status = status ;
        this.code = code;
        this.msg = msg;
        this.data = data;
        this.feature = feature;
    }

    public Boolean getStatus() {
        return status;
    }

    public void setStatus(Boolean status) {
        this.status = status;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Map getFeature() {
        return feature;
    }

    public void setFeature(Map feature) {
        this.feature = feature;
    }

    public boolean isSuccess() {
        return this.code == CommonResultCode.SUCCESS.getCode();
    }

    public String toJson() {
        return StringUtil.toString(JsonUtil.toJson(this));
    }

    @Override
    public String toString() {
        return toJson();
    }
}
